package com.codefish.framework.spring.aop.framework;

import com.codefish.framework.spring.core.utils.AssertUtils;

import java.lang.reflect.Proxy;

/**
 * Aop代理工厂接口的默认实现
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/10/20 下午 08:16
 */
public class DefaultAopProxyFactory implements IAopProxyFactory {

    @Override
    public IAopProxy createAopProxy(AdviseSupport adviseSupport) {
        AssertUtils.notNull(adviseSupport, "adviseSupport不能为null");
        Class<?> targetClass = adviseSupport.getTargetClass();
        AssertUtils.notNull(targetClass, "adviseSupport的targetClass不能为null");
        if (adviseSupport.isProxyTargetClass()) {
            //Aop配置的proxyTargetClass属性为true,使用CGlib代理
            if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
                //即使aop配置的proxyTargetClass属性为true，如果要代理的目标类是个接口
                // 或者这个类是JDK动态代理生成的对象，那么还是要使用JDK动态代理
                return new JdkDynamicAopProxy(adviseSupport);
            }
            return new CglibAopProxy(adviseSupport);
        } else {
            //Aop配置的proxyTargetClass属性为false,使用JDK动态代理
            return new JdkDynamicAopProxy(adviseSupport);
        }
    }

}
